﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PeoplesJukebox.Core.Interfaces;
using PeoplesJukebox.Core.MediaItems;
using System.Threading;
using PeoplesJukebox.Core.Logging;
using config = PeoplesJukebox.Core.Configuration;
using System.Reflection;

namespace PeoplesJukebox.Core.Players
{
    public class WindowsMediaPlayer : IMediaPlayer
    {
        private static Track _currentTrack;
        private static int _mediaErrorCount;
        private static WMPLib.WindowsMediaPlayer _player;

        static WindowsMediaPlayer()
        {
            _mediaErrorCount = 0;
            _player = new WMPLib.WindowsMediaPlayer();
            _player.uiMode = "none";
            _player.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(OnPlayStateChange);   
            _player.MediaError += new WMPLib._WMPOCXEvents_MediaErrorEventHandler(OnMediaError);
        }

        public Track CurrentTrack
        {
            get { return _currentTrack; }
        }

        public void Start()
        {
            PlayNext();
        }

        static void PlayNext()
        {
            Track nextTrack = PeoplesJukeboxService.GetNextTrack();
            if (nextTrack != null)
            {
                try
                {
                    StopInternal();
                    ThreadPool.QueueUserWorkItem(PlayCallback, nextTrack);                    
                }
                catch (Exception ex)
                {
                    Log.Error("WindowsMediaPlayer.PlayNext errored: {0}", ex.Message);
                }
                _currentTrack = nextTrack;
            }
        }

        static void PlayCallback(object state)
        {
            Track nextTrack = state as Track;
            _player.URL = nextTrack.Uri;
            Log.Info("WindowsMediaPlayer.PlayCallback, new URL={0}", _player.URL);
            _player.controls.play();
            Log.Info("WindowsMediaPlayer.PlayCallback, called play()");
        }

        static void OnPlayStateChange(int NewState)
        {
            Log.Info("WindowsMediaPlayer.OnPlayStateChange, new state={0}", (WMPLib.WMPPlayState)NewState);
            if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsMediaEnded)
            {
                PlayNext();
            }
            else
            {
                
            }
        }

        static void OnMediaError(object pMediaObject)
        {
            Log.Error("WMPlayer errored on track {0}, {1}", _currentTrack.Name, pMediaObject);
            _mediaErrorCount++;
            if (_mediaErrorCount < config.PeoplesJukeboxConfiguration.Current.MaxMediaErrorCount)
            {
                PlayNext();
            }
        }

        public void Skip()
        {
            PlayNext();
        }

        public void Stop()
        {
            StopInternal();
        }

        static void StopInternal()
        {
            if (_player != null)
            {
                _player.controls.stop();
            }
        }
    }
}
